package com.base.myproject.client.ui;

import java.util.ArrayList;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.Editdisable;
import com.base.myproject.client.tools.Debug;
import com.base.myproject.client.ui.celleditor.BaseComBoxFromBinding;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.base.myproject.client.ui.celleditor.BaseComboxXBinding;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.core.El;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Widget;

/**
 * form格式的数据显示 初始化结束后一定要记得 formpanel.inited();
 * 
 * @author zgw 2009-6-28 下午11:49:37 javazgw@gmail.com
 */
public class BaseFormPanel extends FormPanel implements LinkParent ,Listener<BaseEvent>{

	BaseStore parentbasestore = null;
	BaseStore basestore = null;
	FormBinding formbinding = null;
	BaseFormPanel formpanel = null;
	FormData fd = new FormData(160, 20);
	String syc = "";
	String maskmessage = "读取数据中...";
	private El disablemask = new El(
	"<div class=\"base-ext-el-mask\" style=\"display: block;\"></div>");
	
	private boolean isdisablebystore = false;
	 final Listener<MessageBoxEvent> l = new Listener<MessageBoxEvent>() {  
	      public void handleEvent(MessageBoxEvent ce) {  
	        Button btn = ce.getButtonClicked();  
	     GWT.log(btn.getText());
	     GWT.log(Dialog.YES);
	      if(btn.getText().equalsIgnoreCase(Dialog.YES))
	      {
	    	  Debug.printf("basestore 2 - "+(basestore==null), 4);
	    	  basestore.addX();
	      }
	      
	      
	      }  
	    };  
	boolean needautoadd = true;
	public BaseFormPanel(BaseStore bs,boolean needautoadd) {
		super();
		this.needautoadd = needautoadd;
		
		setBasestore(bs);
		formpanel = this;


		init();		
		
		
	
		
	
	}

	public BaseFormPanel(BaseStore bs) {
		//super();
		this(bs,true);
//		setBasestore(bs);
//		
//
//		init();
//		formpanel = this;
	}

	public void init() {
		setLayout(new FormLayout());
		sinkEvents(Event.ONCLICK);
		//按照需要制定是否空的时候自动添加
		if(needautoadd)
		{
		addListener(Events.OnClick, new Listener<ComponentEvent>() {

			public void handleEvent(ComponentEvent be) {
				
			//	formpanel.getToolTip().show();
			if((basestore == null ||basestore.getCount()==0)|| formbinding.getModel()==null)
		
				 MessageBox.confirm("提示", "是否添加新内容？", l);  
//				String cls = be.getTarget().getClassName();
//
//				//GWT.log(formbinding.getModel() + "-----" + cls, null);
//				if (formbinding.getModel() == null) {
//					if (cls.indexOf("x-form-field") > 0) {
//						//basestore.addX();
//					}
//
//				}
//
//				// if(cls.equals("title-link")){

			}

		});
		}
		
		
		RowDisable();
	}

	private void RowDisable() {

		if (basestore.isNeedtodeelroweditable() ) {
			addListener(Events.Render, new Listener<BaseEvent>() {

				@Override
				public void handleEvent(BaseEvent be) {
				
					
					if(basestore.getCount()>0 && isIsdisablebystore())
						RowDisable(0);
				}

			});
			
			basestore.addBaseStoreListener(BaseStoreEvent.EventType.ROW_CHANGED,
					new BaseStoreListener() {

						@Override
						public void BaseStoreEventHandle(BaseStoreEvent evt) {
							

							if(basestore.isNeedtodeelroweditable() && isIsdisablebystore())
								RowDisable(evt.getCurrowindex());
						}

						@Override
						public void onBrowserEvent(Event event) {
							// TODO Auto-generated method stub

						}

					});
		}
		
	}

	public boolean add(Widget widget) {

		return add(widget, fd);

	}

	public void inited(int width)
	{
		 for (Field<?> f : getFields()) 
		 {
			 f.setWidth(126);
		 }
		inited();
	}
	/*
	 * 绑定数据一定要等待界面布局完成后进行，否则数据绑定不上去
	 */
	public void inited() {
		formbinding = new FormBinding(this, true){
			  public void autoBind() {
				    for (Field<?> f : panel.getFields()) {
				      if (!bindings.containsKey(f.getId())) {
				        String name = f.getName();
				        if (name != null && name.length() > 0) {
				        	if(f instanceof BaseComboBoxForm<?>)
				        	{
				        		
				        		BaseComBoxFromBinding bcbfb = new BaseComBoxFromBinding((BaseComboBoxForm)f,f.getName());
				        		bindings.put(f.getId(), bcbfb);
				        	}
				        	/**
				        	 * BaseComboBoxX  方式的数据绑定
				        	 */
				        	else if(f instanceof BaseComboBoxX)
				        	{
				        		BaseComboxXBinding bcbxb = new BaseComboxXBinding((BaseComboBoxX)f,f.getName());
				        		bindings.put(f.getId(), bcbxb);
				        	}
				        	else
				        	{
				        		FieldBinding b = new FieldBinding(f, f.getName());
				        		bindings.put(f.getId(), b);
				        	}
				        }
				      }
				    }
				  }

		};
		formbinding.setStore(getBasestore());
		formbinding.addListener(Events.Bind, this);
		formbinding.addListener(Events.UnBind, this);
		formbinding.addListener(Events.BeforeBind, this);
		basestore.addBaseStoreListener(BaseStoreEvent.EventType.LOAD_STARTED,
				new BaseStoreListener() {
					@Override
					public void BaseStoreEventHandle(BaseStoreEvent evt) {

					}

					@Override
					public void onBrowserEvent(Event event) {
						// TODO Auto-generated method stub

					}

				});
		basestore.addBaseStoreListener(BaseStoreEvent.EventType.LOAD_COMPLETE,
				new BaseStoreListener() {

					@Override
					public void BaseStoreEventHandle(BaseStoreEvent evt) {

						formpanel.mask("loading");
						if (basestore.getCount() > 0)
							{
							//formbinding.autoBind();
							formbinding.bind(basestore.getAt(0));
							if(basestore.isNeedtodeelroweditable()&& isIsdisablebystore())
								RowDisable(0);
							}
						else {
							formbinding.unbind();
						}
						// formpanel.el().mask().hide();
						formpanel.unmask();

					}

					@Override
					public void onBrowserEvent(Event event) {
						// TODO Auto-generated method stub

					}

				});
//		basestore.addBaseStoreListener(BaseStoreEvent.EventType.CELL_CHANGED,
//				new BaseStoreListener() {
//
//					@Override
//					public void BaseStoreEventHandle(BaseStoreEvent evt) {
//						// formpanel.el().mask("读取数据中...").show();
//						
//						formbinding.bind(basestore.getAt(evt.getCurrowindex()));
//						// formpanel.el().mask("读取数据中...").hide();
//
//					}
//
//					@Override
//					public void onBrowserEvent(Event event) {
//						// TODO Auto-generated method stub
//
//					}
//
//				});
		basestore.addBaseStoreListener(BaseStoreEvent.EventType.ROW_CHANGED,
				new BaseStoreListener() {

					@Override
					public void BaseStoreEventHandle(BaseStoreEvent evt) {
						formbinding.bind(basestore.getAt(evt.getCurrowindex()));

						if(basestore.isNeedtodeelroweditable() && isIsdisablebystore())
						RowDisable(evt.getCurrowindex());
					}

					@Override
					public void onBrowserEvent(Event event) {
						// TODO Auto-generated method stub

					}

				});
	}

	@Override
	public BaseStore getLinkparent() {
		// TODO Auto-generated method stub
		return parentbasestore;
	}

	@Override
	public void setLinkparent(BaseStore lp) {
		// TODO Auto-generated method stub
		this.parentbasestore = lp;
	}

	public BaseStore getBasestore() {
		return basestore;
	}

	@SuppressWarnings("unchecked")
	public void setBasestore(BaseStore basestore) {
		this.basestore = basestore;
	}

	/**
	 * 绑定
	 * 
	 * @return
	 */
	public FormBinding getFormbinding() {
		return formbinding;
	}

	public void setFormbinding(FormBinding formbinding) {
		this.formbinding = formbinding;
	}

	@Override
	public void handleEvent(BaseEvent be) {
		// TODO Auto-generated method stub
		if(be.getType().equals(Events.BeforeBind))
		{
			
			formpanel.mask(maskmessage);
		}
		else if(be.getType().equals(Events.Bind))
		{
			
			formpanel.unmask();
		}
		else if(be.getType().equals(Events.UnBind))
		{
			
			formpanel.unmask();
		}

	}
	
	@SuppressWarnings("unchecked")
	boolean RowDisable(int rowIndex)
	{
		for(Editdisable edisable :(ArrayList<Editdisable>) (basestore).getEditdisablelist())
		{
			if(edisable.getEdittype().equals(BaseStore.EditDisableType.EQUALS))
			{
				if(basestore.getValue(rowIndex, edisable.getColumnname()) !=null && basestore.getValue( rowIndex,edisable.getColumnname()).equals(edisable.getValue()))
				{
					

					setformdisable(true);
					
					return true;
				}
			}
			else if(edisable.getEdittype().equals(BaseStore.EditDisableType.IN))
			{
				if(basestore.getValue(rowIndex, edisable.getColumnname()) !=null &&  basestore.getValue(rowIndex, edisable.getColumnname()).toString().indexOf(edisable.getValue())>=0)
				{
			
					setformdisable(true);
					
					return true;
				}
			}
			else if(edisable.getEdittype().equals(BaseStore.EditDisableType.LIKE))
			{
				
			}	
		}
		
		

		setformdisable(false);
		
		return false;
	}
	public void setformdisable(boolean b )
	{
		if(b)
		{
			el().appendChild(disablemask.dom);

		addStyleName("baseformdisable");
		}
		else
		{
			disablemask.remove();
			
			removeStyleName("baseformdisable");
		}
		
	}
/**
 * 
 * @return
 */
	public boolean isIsdisablebystore() {
		return isdisablebystore;
	}
/**
 * 和store 的isNeedtodeelroweditable 配合才能生效
 * @param isdisablebystore
 */

	public void setIsdisablebystore(boolean isdisablebystore) {
		this.isdisablebystore = isdisablebystore;
	}
	
}
